import requests
import json
import time
import datetime
import uuid
import random
import string
import base64
import copy
import math

url = "http://127.0.0.1:8000/api"


def timestamps_s():
    return int(time.time())


def timestamps_ms():
    return int(time.time() * 1000)


def today_str():
    return datetime.datetime.now().strftime("%Y-%m-%d")


def time_plus():
    return (((int(time.time() * 1000) * 9301) + 49297) % 233280) / 233280


def random_str(str_len):
    hub = string.ascii_lowercase + string.ascii_uppercase + string.digits

    return ''.join(random.choice(hub) for _ in range(str_len))


def tomorrow_str():
    return (datetime.datetime.now() + datetime.timedelta(days=1)).strftime("%Y-%m-%d")


def flag_rate_unique_str():
    base = ["oreo", "trakinas", "ritz", "prince", "pacific", "chips_ahoy"]

    key_1 = time_plus()
    key_2 = base[int(math.floor(float(key_1) * 10)) % 6]

    return f"{key_1}_0_{key_2}"


def header_cookie():
    fas_uuid = f"{uuid.uuid4()}-{timestamps_ms()}"
    _fas_session_id = f"{random_str(28)}{timestamps_ms()}"
    # 'tujia.com_PortalContext_UserToken=00000000-0000-0000-0000-000000000000; ' \
    #  tujia.com_PortalContext_UserId=0
    today_time = today_str()
    tomorrow_time = tomorrow_str()
    gr_user_id = uuid.uuid4()
    return f'_fas_uuid={fas_uuid}; ' \
           'tj_channel_id=tujia; ' \
           f'gr_user_id={gr_user_id}; ' \
           f'tujia.com_MobileContext_StartDate={today_time}; ' \
           f'tujia.com_MobileContext_EndDate={tomorrow_time}; ' \
           f'tujia.com_PortalContext_UserToken=00000000-0000-0000-0000-000000000000; ' \
           f'tujia.com_PortalContext_UserId=0; ' \
           f'_fas_session_id={_fas_session_id}; ' \
           f'gr_flag={{gr_flag}}'


cookie = header_cookie()
cookie_2 = copy.deepcopy(cookie)
payload = {
    'n': '{"url":"http://m.tujia.com/bingo/h5/house/gethouse/v3/bnb","data":{"houseParameter":{"houseId":16893423,"houseGuid":"","graft":false,"preview":false},"productParameter":{"activityInfo":"","checkInDate":"2021-04-09","checkOutDate":"2021-04-10","needPrice":true,"productId":"","bookingCount":1,"peopleCount":1}}'}

cookie_2 = cookie_2.format(gr_flag=str(base64.b64encode(flag_rate_unique_str().encode('utf-8')), encoding='utf-8'))
response = requests.request("POST", url, data=payload).text
headers1, cookie1 = response.split('^')
headers = eval(headers1)
headers1 = dict()
for i, k in headers.items():
    headers1[i] = str(k)
headers1['Cookie'] = cookie1
headers1[
    'User-Agent'] = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 11_2_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36'
headers1['Accept-Language'] = 'application/json;charset=UTF-8'
headers1['Content-Type'] = 'zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7'

print(headers1)

payload1 = '''{"houseParameter":{"houseId":16893423,"houseGuid":"","graft":false,"preview":false},"productParameter":{"activityInfo":"","checkInDate":"2021-04-09","checkOutDate":"2021-04-10","needPrice":true,"productId":"","bookingCount":1,"peopleCount":1}}'''
# headers1 = {'Connection': 'keep-alive', 'Pragma': 'no-cache', 'Cache-Control': 'no-cache',
#             'LL-INFO': 'wprDtcOLwqEDTQ5Tw74GwrHDu2TDvR4Xwr7CmsKmLhJQDVPDqsKLw6tsa8K5VsOCw4g7K8Oww6QJcFZ0Bn5Owo9IwqgsYMKkwotywpXDu8OxPcOZN8OVw63CssO8wpB/VlfDhC3DpMK8WSfCusOaw6vCm2rDsE8pwqYgw5fDvMOYGMKeJMK6bzHDlcKNw7zDssK7w7ZsworDv1hWwqsxMMKCVMOBw7VrBBZlwo7DgsOLAMK6Xz/CiMK4w6nDn8OhwpHCqXPDhcKawrbDmsKtK8OAwrPDkD51w5/DrMOnw6TColTDkDogwpvCsMKfaSYmew3DoUPCumcrwoHDmsOETMOYQ8O0W8Ocw48Pw7pjw4PCnsKxH28HUQHCv0bDo8OLJcKZPsOEwox5w43CmU09woPDmMOcw7nCh0lGDMKcw7Y7w5rDhMObwoTCpiHDtcOSw7w4w5XDv8KqanlXTcKIw6V+S17DvyXCo8K/G8KFCsOFwr15YcKUBcO1T00OcDk6QMOQwrhWYnzCqgXDrsKOEsKmw4QfS8OvWcOVw4fClDLDhigCBMOVw7LDt8OeRmnDgQJIw6hMFCt2Y8KWw4kiwrzDuMOWJ8Ouw5FvSE99wrcRRsK9woPDpcOfdsOmEkbDmBccwp14w6hRK8KnwrFTMlIuIMOUwoM1aCDCgxhReMOKLMOe',
#             'T-INFO': '62674a99f8a4c00fe292aa38698f1a974362c0054fdd595a31258dcb6697f63fd091cbc4956d2782f28c6ed662e5301eb7b0eb615020b48681d288ff45b185a026778aa51ca62061460299a06fbeaa38z70YYHno7X+tpeTcyZ/HYfsPbipdZCmjFSfbg4OWTuCB8uYBbKyZNXnImndKtVarDC3GAemba1phTK6fmDqMUQaXbbBnEMtE+Q==',
#             'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0', 'X-TJCH': '0',
#             'X-TJH': 'c657c7db5cfb13820c9197ac21c130eab1633b5b', 'X-TJP': '28', 'X-TJTS': '1617958287',
#             'Content-Type': 'application/json;charset=UTF-8', 'Origin': 'https://m.tujia.com',
#             # 'Cookie': '_fas_uuid=105ee3f0-444e-4133-a0d9-65d6dc21e068-1617958137251; tj_channel_id=tujia; gr_user_id=04875c00-1dc2-4893-a990-c5bed5f202cf; tujia.com_MobileContext_StartDate=2021-04-09; tujia.com_MobileContext_EndDate=2021-04-10; tujia.com_PortalContext_UserToken=00000000-0000-0000-0000-000000000000; tujia.com_PortalContext_UserId=0; _fas_session_id=abchjc2KHfh22jz22bPJ9vfGxe5R1617958137251; gr_flag=MC44MjczNjYyNTUxNDQwMzNfMF9yaXR6',
#             'Cookie': cookie1,
#             'X-Tingyun-Id': '-EoKhdMl0Dc;r=958139074', 'Accept-Language': 'zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7'}
response1 = requests.post(
    'http://m.tujia.com/bingo/h5/house/gethouse/v3/bnb',
    headers=headers1,
    data=payload1,
    timeout=5,
    allow_redirects=False,
)
print(response1.text)
